package Resizers;

import org.eclipse.swt.graphics.*;

import Drawers.*;
import Logs.*;
import TimerLogs.*;
import Utils.Functions;

public class NNResizer implements Resizer
{
	private Logger _log;
	
	//No intelization needed for NN.
	public void Init(ImageData originalImage, Point maxSize) {}
	
	public Point CanResize(Point requestedSize)
	{
		return requestedSize;
	}
	
	public void SetLogger(Logger log)
	{
		_log = log;
	}
	
	public void Resize(ImageData originalImage, Drawer drawer, Point trgSize)
	{	
		if (_log == null)
			_log = new DummyLogger();
		
		_log.Start();
		
		_log.Log("Aquiring memory");
		drawer.Start(trgSize, Functions.ClearPalette());
		_log.Log("Aquired memory, staring to interpelate.");
		
		final double xStep = (double)originalImage.width / (double)trgSize.x;
		final double yStep = (double)originalImage.height / (double)trgSize.y;
		
		double oldX = 0;
		for (int x = 0; (x < trgSize.x) && !drawer.ToStop(); x++, oldX += xStep)
		{
			double oldY = 0;
			for (int y = 0; y < trgSize.y; y++, oldY += yStep)
				drawer.PutPixel(x, y, originalImage.getPixel(
						(int)Math.floor(oldX), (int)Math.floor(oldY)));
		}
		
		_log.Log("Finished interpelating.");
		
		drawer.Finish();
		
		_log.Log("Finished blitting");
		_log.Log(String.format(
				"NNeberized from (%d, %d) to (%d, %d)."
				, originalImage.width, originalImage.height, trgSize.x, trgSize.y));
		
		_log.Stop();
	}
}
